package leetcode.weekly.week308;

import java.util.Arrays;

//Solution1Test
public class Solution1 {
	public int[] answerQueries(int[] nums, int[] queries) {
		int ans[] = new int[queries.length];
		Arrays.sort(nums);
		int sums[] = new int[nums.length + 1];
		for (int i = 1; i <= nums.length; i++) {
			sums[i] = nums[i - 1] + sums[i - 1];
		}
		for (int i = 0; i < queries.length; i++) {
			int max = queries[i];
			int l = 0, r = nums.length;
			int cur = 0;
			while (l <= r) {
				int mid = ((r - l) >> 1) + l;
				if (sums[mid] <= max) {
					cur = mid;
					l = mid + 1;
				} else {
					r = mid - 1;
				}
			}
			ans[i] = cur;
		}
		return ans;
	}

}
